<template>
  <div>
    <!-- 顶部导航 -->
    <van-nav-bar
      title="商品详情"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    ></van-nav-bar>
    <!-- 购物车主体 -->
    <div id="gbox">
      <ul>
        <li v-for="(v, i) of this.list" :key="i">
          <!-- 点中触发checked属性 -->
          <!-- 单选框 -->
          <div class="radio">
            <i
              class="iconfont"
              :class="showIcon"
              @click="iconShow = !iconShow"
            ></i>
            <!-- <i
			
							class="iconfont icon-weixuanzhong icon-31xuanzhong"
							
						></i> -->
          </div>
          <!-- 图片 -->
          <div class="img"><img :src="`${v.pic}`" alt="" /></div>
          <!-- 文字信息 -->
          <div class="txt">
            <p>
              {{ v.title }}
              <br />
              <span style="color: rgb(128, 128, 128); font-family: '微软雅黑'">
                {{ v.color_style }} &nbsp;
              </span>
            </p>
            <span>￥{{ v.price }}</span>
          </div>
          <!-- 加减按钮 -->
          <div class="cz">
            <span class="jjbtn" @click="jian(i)">-</span>
            <input type="text" :value="`${v.count}`" class="count" />
            <span class="jjbtn" @click="jia(i)">+</span>
          </div>
          <!-- 删除图标 -->
          <i class="iconfont icon-shanchu" @click="removeHandler(i)"></i>
        </li>
      </ul>
    </div>
    <!-- 提交商品栏 -->
    <van-submit-bar
      :price="totalPrice * 100"
      button-text="提交订单"
      @submit="show = true"
    >
      <!-- <van-checkbox v-model="checked">全选</van-checkbox> -->
      <van-overlay :show="show" @click="show = false">
        <div class="buy"><div class="img"></div></div>
      </van-overlay>
    </van-submit-bar>
  </div>
</template>
<style>
* {
  padding: 0;
  margin: 0;
}
li {
  list-style: none;
}
#gbox {
  width: 100%;
  height: auto;
  overflow: hidden;
}
#gbox ul li {
  width: 100%;
  height: 80px;
  padding: 10px 0;
  background-color: #fff;
  border-bottom: 1px #d7d7d7 solid;
  position: relative;
}
/* 单选框 */
#gbox ul li .radio {
  width: 40px;
  height: 100%;
  float: left;
}
#gbox ul li .radio i {
  display: block;
  width: 18px;
  height: 18px;
  margin: 36px auto;
}
/* 图片 */
#gbox ul li .img {
  width: 80px;
  height: 100%;
  float: left;
}
#gbox ul li .img img {
  display: block;
  width: 70px;
  height: 70px;
  border: 1px #d7d7d7 solid;
  margin: 5px 0;
}
#gbox ul li .txt {
  width: 56%;
  height: 100%;
  float: left;
  font-size: 12px;
}
#gbox ul li .txt p {
  width: 100%;
  height: 54px;
  overflow: hidden;
  line-height: 18px;
}
#gbox ul li .txt span:nth-child(2) {
  width: 100%;
  height: 26px;
  line-height: 26px;
  color: #ff4400;
}
/* 按钮 */
#gbox ul li .cz {
  width: auto;
  height: 25px;
  position: absolute;
  bottom: 10px;
  right: 10px;
  text-align: right;
  line-height: 25px;
  font-size: 14px;
  color: gray;
}
#gbox ul li .cz .count {
  float: left;
  width: 30px;
  height: 25px;
  line-height: 25px;
  border: 1px #999 solid;
  text-align: center;
  -webkit-appearance: none;
  border-radius: 0;
}
#gbox ul li .cz span.jjbtn {
  border: 1px #999 solid;
}
#gbox ul li .cz span {
  display: block;
  float: left;
  width: 25px;
  height: 25px;
  text-align: center;
}
.buy {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}
.buy .img {
  width: 250px;
  height: 300px;
  background-image: url(../../public/ava.jpg);
  background-size: cover;
}
</style>
<script>
import { mapState } from "vuex";
import { mapMutations } from "vuex";
export default {
  data() {
    return {
      // 控制遮罩层
      show: false,
      n: 1,
      //商品全选
      checked: "",
      //单选商品
      // radio: 1,
      //设置一个空数组来装将要加入vuex里面购物车数组的数据
      list: [],
      //控制单个选择的图标
      iconShow: false,
      selectOne: false,
    };
  },
  methods: {
    ...mapMutations(["setShop"]),
    ...mapMutations(["setBadge"]),
    // a(val){
    // 	this.list[val].active = !this.list[val].active
    // 	console.log(val)
    // 	console.log(this.list)
    // },
    onClickLeft() {
      this.$router.go(-1);
    },
    //按钮加减
    jian(i) {
      console.log(`减`);
      if (this.list[i].count > 0) {
        this.list[i].count--;
      }
    },
    jia(i) {
      console.log(`加`);
      this.list[i].count++;
    },
    //删除购物车物品
    removeHandler(i) {
      this.list.splice(i, 1);
      // 6.调用vuex中的setShop方法把当前数组的数据返回vuex
      // this.$store.commit('setShop',this.list)

      // this.setShop(this.list);
      this.setShop(this.list);
    },
    //点击购买后
    buy() {},
  },
  mounted() {
    // 4.将shopCart的数据赋值给list数组，在shopCart组件中使用
    this.list = this.shopCart;
    console.log(this.list);
    // this.list.map(item => {
    // 	item.active = false
    // })
    // this.list[0].active = true
    // this.list[1].active = false
    // 徽章的数字
    this.setBadge(this.list.length);
    console.log(this.list);
  },
  computed: {
    ...mapState(["shopCart"]),
    // 计算总价
    totalPrice: function() {
      return this.list.reduce(function(pre, v) {
        return pre + v.price * v.count;
      }, 0);
    },
    showIcon() {
      return this.iconShow ? "icon-weixuanzhong" : "icon-31xuanzhong";
    },
  },
  watch: {},
};
</script>
